<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="点位id" prop="pointId">
        <el-input
          v-model="queryParams.pointId"
          placeholder="请输入点位id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前温度" prop="currentTemperature">
        <el-input
          v-model="queryParams.currentTemperature"
          placeholder="请输入当前温度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据上报时间" prop="reportTime">
        <el-date-picker clearable
          v-model="queryParams.reportTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择数据上报时间">
        </el-date-picker>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yanjing:temperature:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yanjing:temperature:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yanjing:temperature:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yanjing:temperature:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="temperatureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="点位电杆杆号" align="center" prop="poleCode" />
      <el-table-column label="点位电杆名称" align="center" prop="poleName" />
      <el-table-column label="点位编码" align="center" prop="pointCode" />
      <el-table-column label="点位名称" align="center" prop="pointName" />
      <el-table-column label="点位设备名称" align="center" prop="pointDeviceName" />
      <el-table-column label="相位" align="center" prop="phase" />

      <el-table-column label="当前温度(°C)" align="center" prop="currentTemperature" width="100" >
        <template #default="scope">
          {{ scope.row.currentTemperature !=null && scope.row.currentTemperature != '' ? scope.row.currentTemperature  : "-" }} 
        </template>
      </el-table-column>
      <el-table-column label="当前电压(mV)" align="center" prop="currentVoltage" width="110">
        <template #default="scope">
          {{ scope.row.currentVoltage !=null && scope.row.currentVoltage != '' ? scope.row.currentVoltage : "-" }} 
        </template>
      </el-table-column>
      <el-table-column label="电池状态" align="center" prop="powerStat"  >
              <template #default="scope">
                  <span :style="{ color: getStatusColor(scope.row.powerStat) }">
          {{ getBatteryStatusText(scope.row.powerStat) }}
                  </span>
        </template>
      </el-table-column>
      <el-table-column label="数据上报时间" align="center" prop="reportTime" width="100" />
      <el-table-column label="在线状态" align="center" prop="standby1"  >
              <template #default="scope">
          {{ scope.row.standby1=="1"? "正常" : "离线" }}
        </template>
      </el-table-column>
      <el-table-column label="报警温度配置" align="center" prop="standby2"   />
      <el-table-column label="温度最后改变时间" align="center" prop="standb3" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yanjing:temperature:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yanjing:temperature:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改温度日志对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

          <el-form-item label="点位" prop="pointId">
              <el-select
                v-model="form.pointId"
                placeholder="请选择点位"
                clearable
              >
                <el-option
                  v-for="pointOne in pointList"
                  :key="pointOne.id"
                  :label="pointOne.pointCode"
                  :value="pointOne.id"
                ></el-option>
              </el-select>
            </el-form-item>


        <el-form-item label="当前温度" prop="currentTemperature">
          <el-input v-model="form.currentTemperature" placeholder="请输入当前温度" />
        </el-form-item>
        <el-form-item label="数据上报时间" prop="reportTime">
          <el-date-picker clearable
            v-model="form.reportTime"
            type="datetime"
            placeholder="请选择数据上报时间">
          </el-date-picker>
        </el-form-item>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTemperature, getTemperature, delTemperature, addTemperature, updateTemperature,queryDeviceTemperatureList } from "@/api/line/temperature.js";
import { listPoint } from "@/api/line/point.js";
import { formatDate } from "@/utils/date";

export default {
  name: "Temperature",
  data() {
    return {
      pointList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 温度日志表格数据
      temperatureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pointId: null,
        currentTemperature: null,
        reportTime: null,
        standby1: null,
        standby2: null,
        standb3: null,
        createEmployeeId: null,
        updateEmployeeId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.listPoint();
  },
  methods: {
      getBatteryStatusText(status) {
    switch (status) {
      case "full":
        return "充足";
      case "norm":
        return "正常";
      case "low":
        return "低电压";
      default:
        return "未知";
    }
  },
        getStatusColor(status) {
      switch (status) {
        case "full":
          return "blue";
        case "norm":
          return "green";
        case "low":
          return "red";
        default:
          return "black";
      }
    },
        listPoint() {
      this.loading = true;
      listPoint(this.queryParams).then(response => {
        this.pointList = response.rows;
      });
    },
    /** 查询温度日志列表 */
    getList() {
      this.loading = true;
      queryDeviceTemperatureList(this.queryParams).then(response => {
        this.temperatureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pointId: null,
        currentTemperature: null,
        reportTime: null,
        standby1: null,
        standby2: null,
        standb3: null,
        createTime: null,
        createEmployeeId: null,
        updateTime: null,
        updateEmployeeId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加温度日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTemperature(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改温度日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.reportTime != null && this.form.reportTime != undefined){
        this.form.reportTime = formatDate(this.form.reportTime, "yyyy-MM-dd hh:mm:ss")
      }
      
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTemperature(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemperature(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除温度日志编号为"' + ids + '"的数据项？').then(function() {
        return delTemperature(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yanjing/temperature/export', {
        ...this.queryParams
      }, `temperature_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
